package com.xinjian.jianba.service.impl;

import java.io.IOException;
import java.util.Date;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import com.xinjian.jianba.dto.es.EsServiceResult;
import com.xinjian.jianba.dto.es.OneKeyOnlineDto;
import com.xinjian.jianba.service.EsService;
import com.xinjian.jianba.service.RedisService;
import com.xinjian.jianba.service.UserOnlineService;
import com.xinjian.jianba.utils.EsUtil;
import com.xinjian.jianba.utils.TimeUtil;
@Service
public class UserOnlineServiceImpl implements UserOnlineService{

	@Autowired
	private EsService esService;
	@Override
	public EsServiceResult state(OneKeyOnlineDto oneKeyOnlineDto) throws IOException {
		//更新约见的人的在线状态
		XContentBuilder builder = XContentFactory.jsonBuilder();
		String date=TimeUtil.formatToString(new Date());
		Integer isOnline=oneKeyOnlineDto.getIsOnline();
        builder.startObject();
        {
            builder.field("userId", oneKeyOnlineDto.getUserId());
            builder.field("nickName", oneKeyOnlineDto.getNickName());
            builder.field("isOnline", isOnline);
            if(isOnline.equals(1)){
            	builder.field("onLineTime",date);
            }
        }
        builder.endObject();
        EsServiceResult result= esService.updateDocumentsUserId(EsUtil.oneKeyIndex, EsUtil.oneKeyType, builder,String.valueOf(oneKeyOnlineDto.getUserId()));
        return result;
	}

}
